package cn.tom.controller.adm;

import cn.tom.tools.XResp;
import cn.tom.entity.Type;
import cn.tom.tools.XJson;
import cn.tom.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api/adm/type")
public class AdmTypeController {
    @Autowired
    private TypeService typeService;

    @GetMapping("/findAll2")
    public XJson findType() {
        return XResp.pack(200, "", typeService.findAll());
    }
    //假删除
    @GetMapping("/remove/{id}")
    public XJson remove(@PathVariable int id){
       int n = typeService.remove(id);
       if(n == 0 )return XResp.pack(401,"删除失败","");
        return XResp.pack(200,"删除成功",typeService.findAll());
    }


    @GetMapping("/findById/{id}")
    public XJson findById(@PathVariable int id){
        Type byId = typeService.findById(id);
        if(byId == null)
            return XResp.pack(501,"查找不到",null);
        return XResp.pack(200,"查找成功",byId);
    }

    @PostMapping("/addUp")
    public XJson addUp(Integer id
                      ,String name
                      ,Integer pid
                      ,Integer prio){
        Type type = new Type();
        type.setName(name);
        type.setPrio(prio);
        if(id > 0){
            type.setId(id);
            Type type1 = typeService.update(type);
            if(type1 == null)  return
                    XResp.pack(401,"修改失败",findType().getData());
            return
                    XResp.pack(200,"修改成功",findType().getData());
        }
        //pid > 0 表示添加二层
        if( pid > 0 ) type.setPid(pid);
        int add = typeService.add(type);
        if(add == 0) return
                XResp.pack(401,"添加失败",findType().getData());
        return
                XResp.pack(200,"添加成功",findType().getData());
    }
}
